package com.leo;

import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.scene.control.DialogPane;
import javafx.stage.Stage;

/**
 * @ClassName ScheduledServiceDemo
 * @Description 多任务
 * @Author wangss
 * @date 2020.05.17 23:38
 * @Version 1.0
 */
public class ScheduledServiceDemo extends ScheduledService<Integer> {

    private DialogPane pane;
    private Stage stage;
    private int i = 0;

    public ScheduledServiceDemo(DialogPane pane, Stage stage) {
        this.pane = pane;
        this.stage = stage;
    }

    @Override
    protected Task<Integer> createTask() {
        return new Task<Integer>() {
            @Override
            protected Integer call() throws Exception {
                System.out.println("call = " + Thread.currentThread().getName());
                return i++;
            }

            @Override
            protected void updateValue(Integer value) {
                super.updateValue(value);
                System.out.println("updateValue = " + Thread.currentThread().getName());
                System.out.println("updateValue的值 = " + value);
                if (value <= 10) {
                    pane.setContentText(value + "");
                } else {
                    ScheduledServiceDemo.this.cancel();
                    stage.close();
                }
            }
        };
    }
}
